package dk.lector;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import dk.lector.domain.TSSystemIntent;
import dk.lector.domain.TeamShareMobile;
import dk.lector.domain.entities.User;

public abstract class BaseFullScreenActivity extends BaseActivity {


	private String TAG = this.getClass().getSimpleName();
	private BroadcastReceiver stateChangedReceiver;
	private boolean initialized;
	private boolean configured;
	private User currentUser;
	private long lastUpdate;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
			
		
		if(getIntent().hasExtra("globals")){
			variablesFromBundle(getIntent().getBundleExtra("globals"));
				
		}
		handleState((TeamShareMobile) getApplicationContext());
		
		stateChangedReceiver = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				handleState((TeamShareMobile) getApplicationContext());
			}};
	
			
	}
		
		
	public Bundle getAvtivityState()
	{
		return variablesToBundle();
	}
	public User getAvtivityUser()
	{
		return currentUser;
	}
	
	private void variablesFromBundle(Bundle globalState)
	{
	currentUser = (User) globalState.get("currentUser");
	configured = globalState.getBoolean("configured");
	initialized = globalState.getBoolean("initialized");
	lastUpdate = globalState.getLong("lastUpdate");
		
	}
	
	private Bundle variablesToBundle()
	{
		Bundle state = new Bundle();
		state.putParcelable("currentUser", currentUser);
		state.putBoolean("configured", configured);
		state.putBoolean("initialized", initialized);
		state.putLong("lastUpdate", lastUpdate);
		return state;
		
	};
	
	
	@Override
	protected void onPause() {
		unregisterReceiver(stateChangedReceiver);
		super.onPause();
	}
		
	
	@Override
	protected void onResume() {
		Log.d(TAG, "onResume()");
		registerReceiver(stateChangedReceiver, new IntentFilter(getString(R.string.ACTION_STATE_CHANGED)));
		if(currentUser != null)
		{
		TSSystemIntent intent = new TSSystemIntent(R.string.ACTIONCODE_USER_ACTIVITY, TAG);
		intent.putExtra("userID", currentUser.getId());
		sendBroadcast(intent);
		}
		
		super.onResume();
	
	}
	
	public void handleState(TeamShareMobile core) {
		
		variablesFromBundle(core.getState());
		
		if(!configured) {
			Intent configIntent = new Intent(this, ConfigurationActivity.class);
			configIntent.setData(getIntent().getData());
			startActivity(configIntent);
		}
		
		else{
			Log.d(TAG, "SOMETHING CALLED THIS; CHECK HOW OFTEN?!");
			if(currentUser == null)
			{
			if(core.getUser() == null){	
			Intent loginIntent = new Intent(this, LoginActivity.class);
			loginIntent.setData(getIntent().getData());
			startActivity(loginIntent);
			}
			}
			
		}
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		((TextView)findViewById(R.id.actionbar_textview)).setText(actionBarText());
	}
	
	public abstract String actionBarText();
	
	public void goHome(View v){
		Intent dashBoardIntent = new Intent(this, DashBoardActivity.class);
		startActivity(dashBoardIntent);
	}
	

}

	


